package com.plian.system.service.sys.office.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.StringUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.cache.OfficeCache;
import com.plian.system.dao.sys.user.UserDao;
import com.plian.system.entity.sys.Office;
import com.plian.system.entity.sys.User;
import com.plian.system.mapper.sys.OfficeMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.properties.GzwProperties;
import com.plian.system.service.sys.office.IOfficeService;
import com.plian.system.service.sys.orgzation.IOrgService;
import com.plian.system.service.sys.user.ISysUserService;
import com.plian.system.vo.sys.OfficeVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO
 * @Date: 2022/12/21 4:07 PM
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class OfficeServiceImpl extends BaseServiceImpl<OfficeMapper, Office> implements IOfficeService {

    private UserDao userDao;

    private ISysUserService sysUserService;

    private GzwProperties gzwProperties;

    private IOrgService orgService;

    private ISysUserService userService;


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean saveOrUpdate(Office entity) {
        boolean result = super.saveOrUpdate(entity);
        List<String> userIds = entity.getUserIds();
        if (CollectionUtil.isNotEmpty(userIds)) {
            Long officeId = entity.getId();
            userIds.forEach(id -> {
                PageData pageData = new PageData();
                pageData.put("officeId", officeId);
                pageData.put("id", id);
                userDao.editOffid(pageData);
            });
        }
        OfficeCache.update(entity.getId());
        return result;
    }

    @Override
    public boolean deleteLogic(List<Long> ids) {
        //删除处室下的人员关系
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        userService.update(updateWrapper.in(User::getOfficeId, ids).set(User::getOfficeId,null));
        return super.deleteLogic(ids);
    }

    @Override
    public boolean submit(Office office){
        if (Optional.ofNullable(office.getId()).isPresent()){
            // 删除处室下的人员关系
            LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
            userService.update(updateWrapper.eq(User::getOfficeId, office.getId()).set(User::getOfficeId,null));
        }
        return saveOrUpdate(office);
    }

    /**
     * @Author: Take-off
     * @Description: 获取详情
     * @Date: 5:49 PM 2023/1/3
     * @Param: [officeVO]
     * @return: com.plian.system.vo.sys.OfficeVO
     **/
    @Override
    public OfficeVO getDetail(OfficeVO officeVO) {
        List<User> userList = sysUserService.list(new LambdaQueryWrapper<User>().eq(User::getOfficeId, officeVO.getId()));
        if (CollectionUtil.isNotEmpty(userList)) {
            officeVO.setUserIds(userList.stream().map(User::getId).collect(Collectors.toList()));
            officeVO.setUserNames(userList.stream().map(User::getName).collect(Collectors.toList()));
        }
        return officeVO;
    }

    /**
     * @Author: Take-off
     * @Description: 是否考核处
     * @Date: 11:14 AM 2023/1/28
     * @Param: [officeId]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean checkAssess(String officeId) {
        Boolean result = false;
        if (Optional.ofNullable(officeId).isPresent() && officeId.equals(gzwProperties.getAssessmentId())) {
            result = true;
        }
        return result;
    }

    /**
     * 判断是否为财管处
     *
     * @param officeId
     * @return java.lang.Boolean
     **/
    @Override
    public Boolean checkFinancialManagement(String orgId, String officeId) {
        Boolean result = false;
        if (StringUtil.isNotBlank(orgId)) {
            //判断是否为国资委,非国资委直接返回false
            Boolean gzwFlag = orgService.checkIsGzw(orgId);
            if (!gzwFlag) {
                return result;
            }
        }
        if (Optional.ofNullable(officeId).isPresent() && officeId.equals(gzwProperties.getFinancialManagementId())) {
            result = true;
        }
        return result;
    }
}
